package com.hyh.admin.tool;


import com.hyh.ad.common.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 *  热门搜索工具类
 * @date 2024-06-20
 */

@Component
public class HotSearchUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    // 更新搜索结果次数
    public void updateSearchResult(String result) {
        // 用redis中的zset数据结构，用于存储热门搜索结果
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.incrementScore(Constants.HOT_SEARCH_KEY, result, 1);
    }

    // 获取前10条热门搜索结果
    public Set<String> getTopSearchResults() {
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        return zSetOperations.reverseRange(Constants.HOT_SEARCH_KEY, 0, 9);
    }
}
